package com.example.demo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.demo.databinding.ActivityMainBinding;

import java.io.IOException;
import java.lang.ref.WeakReference;

public class MainActivity extends AppCompatActivity {
    ActivityMainBinding binding;
    MyHandler myHandler;
    PlayListAdapter adapter;
    LoginState loginState;

    static enum LoginState {
        LOGINING_STATE,
        LOGIN_STATE,
        LOGOUT_STATE,
    };

    NetworkUtil networkUtil;

    static protected class MyHandler extends Handler {
        WeakReference<MainActivity> mActivity;
        MyHandler(MainActivity activity) {
            mActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Contant.MSG_DEBUG_INFO: {
                    MainActivity activity = mActivity.get();
                    if (activity != null) {
                        activity.setInfo((String) msg.obj);
                    }
                }
                break;

                case Contant.MSG_SERVER_LOGIN: {
                    MainActivity activity = mActivity.get();
                    if (activity != null) {
                        activity.loginState = LoginState.LOGIN_STATE;
                        activity.binding.btnConnectId.setText("断开连接");
                        activity.binding.btnConnectId.setEnabled(true);

                        activity.setInfo("已连接服务器");
                    }
                }
                break;

                case Contant.MSG_SERVER_LOGOUTING: {
                    MainActivity activity = mActivity.get();
                    if (activity != null) {
                        activity.loginState = LoginState.LOGOUT_STATE;
                        activity.binding.btnConnectId.setText("连接服务器");
                        activity.binding.btnConnectId.setEnabled(true);

                        activity.setInfo("正在断开连接...");
                    }
                }
                break;

                case Contant.MSG_SERVER_LOGOUT: {
                    MainActivity activity = mActivity.get();
                    if (activity != null) {
                        activity.networkUtil.cancel(true);
                        activity.networkUtil = null;

                        activity.setInfo("已断开连接");
                    }
                }
                break;
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        loginState = LoginState.LOGOUT_STATE;
        myHandler = new MyHandler(this);

        setContentView(binding.getRoot());
        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        binding.btnConnectId.setText("连接服务器");
        binding.btnConnectId.setEnabled(true);

        adapter = new PlayListAdapter();
        binding.rcyleId.setLayoutManager(new androidx.recyclerview.widget.LinearLayoutManager(this));
        binding.rcyleId.setAdapter(adapter);

        binding.btnConnectId.setOnClickListener(v -> {
            if (loginState == LoginState.LOGOUT_STATE) {
                loginState = LoginState.LOGINING_STATE;
                networkUtil = new NetworkUtil(myHandler);
                networkUtil.execute();
                binding.btnConnectId.setText("断开连接");
                binding.btnConnectId.setEnabled(false);

                setInfo("正在连接服务器...");
            }else if (loginState == LoginState.LOGIN_STATE) {
                networkUtil.logout();
            }
        });

        binding.btnPlayId.setOnClickListener(v -> {

        });
    }

    protected void setInfo(String info) {
        // TODO: append info to the end of multiline text view's content
        binding.edtId.append(info + "\n");
    }
}